import 'package:demo/extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:cached_network_image/cached_network_image.dart';

import 'dart:ui' as ui;

import 'package:flutter/services.dart';

import '../models/fcl_share_item.dart';

class FlsCommonWidget {

  /// 将Widget转换为图片
  static Future<Uint8List> widgetToImage(
    Widget widget, {
    Alignment alignment = Alignment.center,
    Size size = const Size(300, 130),
    double devicePixelRatio = 1.0,
    double pixelRatio = 1.0,
  }) async {
    RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();

    RenderView renderView = RenderView(
      child: RenderPositionedBox(alignment: alignment, child: repaintBoundary),
      configuration: ViewConfiguration(
        size: size,
        devicePixelRatio: devicePixelRatio,
      ),
      window: WidgetsBinding.instance.platformDispatcher.views.first,
    );

    PipelineOwner pipelineOwner = PipelineOwner();
    pipelineOwner.rootNode = renderView;
    renderView.prepareInitialFrame();

    BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
    RenderObjectToWidgetElement rootElement = RenderObjectToWidgetAdapter(
      container: repaintBoundary,
      child: Directionality(
        textDirection: TextDirection.ltr, // 设置适当的阅读方向
        child: widget,
      ),
    ).attachToRenderTree(buildOwner);
    buildOwner.buildScope(rootElement);
    buildOwner.finalizeTree();

    pipelineOwner.flushLayout();
    pipelineOwner.flushCompositingBits();
    pipelineOwner.flushPaint();

    ui.Image image = await repaintBoundary.toImage(pixelRatio: pixelRatio);
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List? pngBytes = byteData?.buffer.asUint8List();
    return pngBytes!;
  }

  /// 将图片转换为Uint8List
  Future<Uint8List> imageToUint8List(String imagePath) async {
    ByteData imageBytes = await rootBundle.load(imagePath);
    return Uint8List.view(imageBytes.buffer);
  }
}

class FlsPaoPaoMarkerWidget extends StatelessWidget {
  FclShareItem item;
  Size size;

  FlsPaoPaoMarkerWidget({super.key, 
  required this.item, 
  this.size = const Size(180, 280)});

    @override
  Widget build(BuildContext context) {
    return Container(
      width: size.width,
      height: size.height,
      color: Colors.red,
      child: Column(
        children: [
          Stack(
            children: [
              buildImage(item.isSelf()?'map/chat_location_map_avatar_icon_background_me':'map/chat_location_map_avatar_icon_background_other', BoxFit.cover, const Size(180, 180)),
               Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: const EdgeInsets.only(top:25.0),
                  child:  ClipOval(
                    child: Image.network(
                      'https://bdcs-file-1.ykt.cbern.com.cn/bdxcloud/avatars/452799072531_AA22426E-7E4C-4F31-8BFA-8DD2EEC35114.jpg!m240x240.jpg',
                      width: 110,
                      height: 110,
                      fit: BoxFit.cover,
                    ),
                    //  child: CachedNetworkImage(
                    // imageUrl: 'https://bdcs-file-1.ykt.cbern.com.cn/bdxcloud/avatars/452799072531_AA22426E-7E4C-4F31-8BFA-8DD2EEC35114.jpg!m240x240.jpg',
                    // fit: BoxFit.cover,
                    // width: 110,
                    // height: 110,
                    // errorWidget: (context, url, error) {
                    //   return Container(
                    //     width: size.width,
                    //     height: size.height,
                    //     color: Colors.grey,
                    //     child: const Center(
                    //       child: Icon(Icons.error),
                    //     ),
                    //   );
                    // }),
                    // child: buildImage('map/chat_def_avatar_icon', BoxFit.contain, const Size(110, 110)),
                  ),
                ),
              )
            ],
          ),
          const SizedBox(height: 10),
          buildImage(item.isSelf()?'map/chat_location_map_avatar_icon_sign_me':'map/chat_location_map_avatar_icon_sign_other', BoxFit.cover, const Size(60, 84)),
        ],
      ),
    );
  }
  

  Widget buildImage(String assetPath, BoxFit fit, Size size) {
    return Image.asset(
      assetPath.img, // 图片的路径
      fit: fit,
      width: size.width,
      height: size.height,
    );
  }
}
